package com.wc.算法提高课.F第六章_基础算法.RMQ.天才的记忆;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/2 13:59
 * @description https://www.acwing.com/problem/content/description/1275/
 */
public class Main {
    /**
     * 思路：
     * 如果会线段树, 这个题会非常简单
     * 倍增的思想
     * f[i][j] 表示从i ~ i + 2^j - 1 之间的最大值一共是2^j次方个数
     * f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1])
     * 如果需要求 [l, r] 之间的最大值
     * f[l][k], f[r - (1 << k) + 1][k]
     * k表示不超过k的最大数的2的指数明
     * k = 下取整log_2 ^ len
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    // log_2^N = 18 左右
    static int N = 200010, M = 18;
//    static long INF = (long) 1e16;
    static long[] a = new long[N];
    static long[][] f = new long[N][M];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextLong();
        init();
        m = sc.nextInt();
        while (m-- > 0) {
            int l = sc.nextInt(), r = sc.nextInt();
            out.println(query(l, r));
        }
        out.flush();
    }

    static long query(int l, int r) {
        int len = r - l + 1;
        int k = (int) (Math.log(len) / Math.log(2));
        return Math.max(f[l][k], f[r - (1 << k) + 1][k]);
    }

    static void init() {
        for (int j = 0; j < M; j++) {
            for (int i = 1; i + (1 << j) - 1<= n; i++) {
                if (j == 0) f[i][j] = a[i];
                else {
                    f[i][j] = Math.max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
                }
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
